package com.dhf.framework.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dhf.framework.base.mybatis.core.page.Pagination;
import com.dhf.framework.common.BaseController;
import com.dhf.framework.system.entity.SysTableInfo;
import com.dhf.framework.system.service.CommonService;
import com.dhf.framework.system.service.SysTableInfoService;

/**
 * </b>SysTableInfoController<br>
 * 表信息
 * 
 * @author minstar
 * @since2017年03月13日 16时51分32秒 星期一
 * @version:1.0
 */
@Controller
@RequestMapping("/system/sysTableInfo")
public class SysTableInfoController extends BaseController {
	private static Logger LOGGER = LoggerFactory.getLogger(SysTableInfoController.class);

	@Autowired
	private SysTableInfoService sysTableInfoService;

	@Autowired
	private CommonService commonService;

	@Value("${tableSchema}")
	private String tableSchema;

	/**
	 * 
	 * @Title: queryPageList @Description: 列表页面 @param @param
	 * searchEntity @param @param pagination @return ModelAndView 返回类型 @throws
	 */
	@RequestMapping(value = "/queryPageList")
	public ModelAndView queryPageList(SysTableInfo sysTableInfo, Pagination<SysTableInfo> pagination) {
		LOGGER.debug(SysTableInfoController.class.getName() + ":开始查询分页数据！");
		ModelAndView mv = new ModelAndView("system/sysTableInfo/sysTableInfoList");
		pagination = this.sysTableInfoService.queryPageList(sysTableInfo, pagination);
		mv.addObject("pagination", pagination);
		return mv;
	}

	/**
	 * 
	 * @Title: toMerge @Description: 新增或修改页面 @param @param entity @return
	 * ModelAndView 返回类型 @throws
	 */
	@RequestMapping(value = "/toMerge")
	public ModelAndView toMerge(SysTableInfo entity) {
		ModelAndView mv = new ModelAndView("system/sysTableInfo/sysTableInfoMerge");
		if (entity.getId() != null) {
			SysTableInfo sysTableInfo = this.sysTableInfoService.get(entity.getId());
			mv.addObject("sysTableInfo", sysTableInfo);
		} else {
			mv.addObject("sysTableInfo", new SysTableInfo());
		}
		return mv;
	}

	/**
	 * 
	* @Description: 新增或修改数据 
	* @param  request
	* @param  entity
	* @param  redirectAttributes
	* @param  设定文件
	* @return ModelAndView    返回类型
	 */
	@RequestMapping(value = "/doMerge")
	public ModelAndView doMerge(HttpServletRequest request, SysTableInfo entity,
			RedirectAttributes redirectAttributes) {
		if (entity.getId() != null) {
			LOGGER.debug(SysTableInfoController.class.getName() + ":开始更新数据！");
			this.addMessage(redirectAttributes, "修改数据成功");
			entity.setModifyTime(new Date());
			this.sysTableInfoService.update(entity);
		} else {
			LOGGER.debug(SysTableInfoController.class.getName() + ":开始新增数据！");
			entity.setCreateTime(new Date());
			this.sysTableInfoService.add(entity);
			this.addMessage(redirectAttributes, "新增据成功");
		}
		return new ModelAndView("redirect:/system/sysTableInfo/queryPageList.do");
	}

	/**
	 * 
	* @Description: 删除数据
	* @param @param id
	* @param @param redirectAttributes
	* @param @return    设定文件
	* @return ModelAndView    返回类型
	 */
	@RequestMapping(value = "/delete")
	public ModelAndView delete(Long id, RedirectAttributes redirectAttributes) {
		LOGGER.debug(SysTableInfoController.class.getName() + ":开始删除数据！");
		this.sysTableInfoService.delete(id);
		this.addMessage(redirectAttributes, "删除数据成功");
		return new ModelAndView("redirect:/system/sysTableInfo/queryPageList.do");
	}

	/**
	 * 
	* @Description: 同步表信息
	* @param @param redirectAttributes
	* @param @return    设定文件
	* @return ModelAndView    返回类型
	 */
	@RequestMapping(value = "/sysTableInfo")
	public ModelAndView sysTableInfo(RedirectAttributes redirectAttributes) {
		LOGGER.debug(SysTableInfoController.class.getName() + ":开始同步数据！");
		List<Map<String, Object>> list = this.commonService.queryAllTables(tableSchema);
		for (Map<String, Object> map : list) {
			String tableName = MapUtils.getString(map, "table_name", "");
			String tableComment = MapUtils.getString(map, "table_comment", "");
			SysTableInfo searchEntity = new SysTableInfo();
			searchEntity.setTableName(tableName);
			List<SysTableInfo> searchList = this.sysTableInfoService.query(searchEntity);
			if (StringUtils.isNotEmpty(tableName) && CollectionUtils.isEmpty(searchList)) {
				SysTableInfo entity = new SysTableInfo();
				entity.setCreateTime(new Date());
				entity.setTableName(tableName);
				entity.setTableComment(tableComment);
				this.sysTableInfoService.add(entity);
			}else{
				LOGGER.warn("表"+tableName+"数据存在！");
			}
		}
		this.addMessage(redirectAttributes, "同步数据成功");
		return new ModelAndView("redirect:/system/sysTableInfo/queryPageList.do");
	}
}
